var template = `
<div class="ins-crack">
    <h3>破解管理</h3>
    <button @click="doAdd" v-if="!isEdit && list.length === 0">新增</button>
    <button @click="doCancel" v-if="isEdit">取消</button>
    <button @click="doRefresh">刷新</button>

    <Form :data="editModel" :showModel="formShowModel" :schema="schema" v-if="isEdit" @cancel="doCancel" @submit="doSubmit"></Form>
    <List :list="list" :showModel="listShowModel" :schema="schema">
        <template v-slot="{item}">
            <button @click="doEdit(item)">修改</button>
            <button @click="doDelete(item)">删除</button>
        </template>
    </List>
</div>
`;


let InsCrack = {
    name: 'InsCrack',
    data() {
        return {
            list: [],
            isEdit: false,
            schema: '破解配置',
            model: {
                crackName: 'ins破解',
                crackCode: `
        async function doCrack(__this,axios) {
            /*
            入参说明:
                __this 当前调用处this,是一个vue组件中的this
                axios               axios对象,用于发起http/s请求
            内置函数说明:
                logMsg(msg)         打印日志,页面上显示破解实时日志
                saveHash(queryHash) 保存得到的queryHash
            */
            /* 获取内置函数,这行不可删改 */
            let {logMsg,saveHash} = __this

            logMsg('开始破解[queryHash]')
            let home=await axios.get('/')
            let parser=new DOMParser()
            let dom=parser.parseFromString(home.data,'text/html')
            let scripts=dom.getElementsByTagName('script')
            let srcArr=Array.from(scripts).map(e=>e.src).filter(e=>e)
            let allJs=[]
            for(i in srcArr){
                try{
                    let resJs=await axios.get(srcArr[i]);
                    allJs.push(resJs.data)
                }catch(e){
                    logMsg('下载js失败',srcArr[i])
                }
            }
            let matchJs=allJs.filter(e=>e.contains('("PolarisProfilePostsActions",'))
            let queryHash=''
            let match = false
            for(i in matchJs){
                let js=matchJs[i]
                try {
                    let h1=js.substr(js.indexOf('function',js.indexOf('("PolarisProfilePostsActions",')),2000)
                    queryHash = h1.substr(h1.indexOf('h="')+3,32)
                    match = /^\w+$/.test(queryHash)
                } catch (e) {
                    console.log('破解异常 err=' + String(err))
                    console.log(err)
                }
                if (match) {
                    console.log('已获取queryHash',queryHash)
                    saveHash(queryHash)
                    break;
                }
            }

            if(match){
                logMsg('破解成功[queryHash]'+queryHash)
            }else{
                logMsg('破解失败[queryHash]'+queryHash)
            }

        }
        /* 导出破解方法,这行不可删改 */
        that.that = doCrack;`,
                enable: true,
            },
            listShowModel: {
                crackName: {
                   type: 'text',
                   maxLength: 15,
                },
                crackCode: {
                   type: 'text',
                   maxLength: 50,
                },
                enable: {
                    type: 'boolean',
                    default: true,
                },
            },
            formShowModel: {
                crackName: {
                   type: 'text',
                   maxLength: 15,
                },
                crackCode: {
                   type: 'textarea',
                   maxLength: 50,
                },
                enable: {
                    type: 'boolean',
                    default: true,
                },
            },
            editModel: {}
        }
    },
    components: {List, Form},
    template,
    mounted: () => {
        console.log('ins-crack mounted');
        const {proxy} = Vue.getCurrentInstance();
        proxy.doGetAll();
    },
    methods: {
        doRefresh() {
            this.$api.insCrack.getAll().then(res => {
                if (res.data && res.data.code === 0) {
                    this.list = res.data.data;
                    let msg = '刷新成功';
                    this.$message({message: msg});
                } else {
                    let msg = '接口调用失败';
                    console.error(msg, res);
                    this.$message({message: msg, level: 'error'});
                }
            }).catch(e => {
                let msg = '接口调用失败!';
                console.error(msg, e);
                this.$message({message: msg, level: 'error'});
            })
        },
        doGetAll() {
            this.$api.insCrack.getAll().then(res => {
                if (res.data && res.data.code === 0) {
                    this.list = res.data.data;
                } else {
                    let msg = '接口调用失败';
                    console.error(msg, res);
                    this.$message({message: msg, level: 'error'});
                }
            }).catch(e => {
                let msg = '接口调用失败!';
                console.error(msg, e);
                this.$message({message: msg, level: 'error'});
            })
        },
        doAdd() {
            this.editModel = deepClone(this.model);
            this.isEdit = true;
        },
        doEdit(item) {
            this.editModel = deepClone(item);
            this.isEdit = true;
        },
        doDelete(item) {
            this.$api.insCrack.delete(item).then(res => {
                if (res.data && res.data.code === 0) {
                    let msg = '删除成功!';
                    console.log(msg, res.data);
                    this.$message({message: msg});
                    this.isEdit = false;
                    this.doGetAll()
                } else {
                    let msg = '删除失败!';
                    console.error(msg, res.data);
                    this.$message({message: msg, level: 'error'});
                }
            }).catch(e => {
                let msg = '删除失败!';
                console.error(msg, e);
                this.$message({message: msg, level: 'error'});
            })
        },
        doSubmit() {
            this.$api.insCrack.update(this.editModel).then(res => {
                if (res.data && res.data.code === 0) {
                    let msg = '保存成功!';
                    console.log(msg, res.data);
                    this.$message({message: msg});
                    this.isEdit = false;
                    this.doGetAll()
                } else {
                    let msg = '保存失败!';
                    console.error(msg, res.data);
                    this.$message({message: msg, level: 'error'});
                }
            }).catch(e => {
                let msg = '保存失败!';
                console.error(msg, e);
                this.$message({message: msg, level: 'error'});
            })
        },
        doCancel() {
            this.isEdit = false;
        },
        doStart() {
            alert('start')
        }
    }
};